package com.cptravel.activity.hot;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseFragment;
import com.core.util.B5MActivityHelper;
import com.core.widget.pulltorefresh.PullToRefreshListView;
import com.cptravel.R;
import com.cptravel.activity.HomeActivity;
import com.cptravel.adapter.HotAdapter;
import com.cptravel.api.GetListRequest;
import com.cptravel.api.GetListResponse;
import com.cptravel.api.GetserveltclassRequest;
import com.cptravel.api.GetserveltclassResponse;
import com.cptravel.bean.Class;
import com.cptravel.bean.Product;

import java.util.ArrayList;
import java.util.List;

public class HotFragment extends B5MBaseFragment implements OnClickListener
{
    private HotAdapter hotAdapter;
    private PullToRefreshListView mPullToRefreshListView;
    private GetListRequest mGetListRequest;
    private PopupWindow mPopupWindow;
    private GetserveltclassRequest mGetserveltclassRequest;
    private List<Class> classes;
    private ListView listView;
    private ArrayList<String> clasStrings;
    private ArrayAdapter<String> arrayAdapter;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.hot;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        bt_left = (Button) containerView.findViewById(R.id.bt_head_left);
        Drawable drawable = getResources().getDrawable(R.drawable.account_notice_down_white_arr);
        bt_left.setText("全部");
        bt_left.setBackgroundDrawable(null);
        drawable.setBounds(0, 0, 50, 30);
        bt_left.setCompoundDrawables(null, null, drawable, null);
        bt_right = (Button) containerView.findViewById(R.id.bt_head_right);
        tv_title = (TextView) containerView.findViewById(R.id.tv_head_title);
        tv_title.setText("热点");
        mProgressBar = (ProgressBar) containerView.findViewById(R.id.pb_head);
        bt_left.setOnClickListener(this);
        mPullToRefreshListView = (PullToRefreshListView) containerView.findViewById(R.id.pull_refresh_list);
        mPullToRefreshListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                // TODO Auto-generated method stub
                Product product = hotAdapter.getList().get((int) arg3);
                Bundle bundle = new Bundle();
                bundle.putSerializable("product", product);
                B5MActivityHelper.startActivity(mActivity, HotDetailActivity.class, bundle);
            }
        });
        listView = new ListView(mContext);
        listView.setDivider(getResources().getDrawable(R.drawable.listview_divider));
        listView.setDividerHeight(2);
        clasStrings = new ArrayList<String>();
        initPopupWindow();
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        hotAdapter = new HotAdapter(mActivity, new ArrayList<Product>());
        classes = new ArrayList<Class>();
        mPullToRefreshListView.setAdapter(hotAdapter);
        mGetListRequest = new GetListRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                GetListResponse getListResponse = (GetListResponse) b5mBaseResponse;
                hotAdapter.getList().clear();
                hotAdapter.setList(getListResponse.products);
                hotAdapter.notifyDataSetChanged();
            }
        }, mProgressBar);
        mGetserveltclassRequest = new GetserveltclassRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                GetserveltclassResponse getserveltclassResponse = (GetserveltclassResponse) b5mBaseResponse;
                classes.clear();
                classes.addAll(getserveltclassResponse.classes);
                clasStrings.clear();
                for (Class class1 : classes)
                {
                    clasStrings.add(class1.title);
                }
                arrayAdapter.notifyDataSetChanged();
            }
        }, mProgressBar);
    }

    protected void initPopupWindow()
    {
        // TODO Auto-generated method stub
        if (mPopupWindow == null)
        {
            mPopupWindow = new PopupWindow(mContext);
            arrayAdapter = new ArrayAdapter<String>(mContext, R.layout.hot_pop, clasStrings);
            listView.setAdapter(arrayAdapter);
            mPopupWindow.setContentView(listView);
            mPopupWindow.setWidth(150);
            mPopupWindow.setHeight(300);
            mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.white));
            mPopupWindow.getBackground().setAlpha(180);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setFocusable(true);
            listView.setOnItemClickListener(classItemClickListener);
        }
    }

    OnItemClickListener classItemClickListener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub
            mPopupWindow.dismiss();
            Class clazz = classes.get((int) arg3);
            mGetListRequest.classid = clazz.classid;
            hotAdapter.getList().clear();
            mGetListRequest.start();
        }
    };

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
        mGetListRequest.ishot = "1";
    }

    @Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        if (hotAdapter.getList().isEmpty()&&!mGetListRequest.isStarted())
        {
            mGetListRequest.start();
        }
        if (classes.isEmpty()&&!mGetserveltclassRequest.isStarted())
        {
            mGetserveltclassRequest.start();
        }
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.bt_head_left:
                if (mPopupWindow.isShowing())
                {
                    mPopupWindow.dismiss();
                }
                else
                {
                    mPopupWindow.showAsDropDown((View) bt_left.getParent());
                }
                break;
            case R.id.bt_head_right:
                ((RadioButton) ((HomeActivity) getActivity()).rgs.getChildAt(2)).setChecked(true);
                break;
            default:
                break;
        }
    }
}
